package com.cy.pj.common.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
import java.util.Map;

@Slf4j
public class TimeAccessInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LocalTime now=LocalTime.now();
        int hour = now.getHour();
        log.debug("hour={}",hour);
        if (hour>=6 && hour<=11){
            throw new RuntimeException("请在其他时间点访问");
        }
        return true;
    }

    public boolean testRequestInfo(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        log.info("当前拦截的方法为：{}",handlerMethod.getMethod().getName());
        log.info("当前拦截的方法参数长度为：{}",handlerMethod.getMethod().getParameters().length);
        log.info("当前拦截的方法的类为：{}",handlerMethod.getBean().getClass().getName());
        System.out.println("开始拦截---------");
        Map<String,String[]> map=request.getParameterMap();
        System.out.println("request.param.map.keys="+map.keySet());
        String uri = request.getRequestURI();
        System.out.println("拦截的uri："+uri);
        return false;
    }
}
